// pages/question/question.js
var minute=0;
var second1=0;
var second2=0;
var value="";
var button=1;
var endX=200;
var endY=586;
var touchmove=0;
var touchend=0;
var pathvoice="";
var switchVoice=0;
var pathimg=[];
var voicetime=0;
var endvoice;
var timer;
var wxNum;
var voicetimer;
var voice=0;
var image=0;
var userImg=[];
var answers="";
var qiniuUploader = require("../qiniuUploader.js");
var time = require('../../utils/util.js');
var questionId;
var masterId;
var uploadimage = function (res) {

  for (var i = 0; i < res.length; i++) {

    qiniuUploader.upload(res[i], (res) => {
      console.log("上传成功")
    },
      (error) => {
        console.log("上传失败")
      },
      {
        uploadURL: 'https://up.qiniup.com',
        region: 'ECN',

        domain: 'www.liuxuanlong.club',
        uptokenURL: 'https://www.liuxuanlong.xyz/fenda/wx/upToken'
      }
    );

  }
}
var uploadvoice = function (res) {
  if (res != null) {
    qiniuUploader.upload(res, (res) => {
      console.log("上传成功")
    },
      (error) => {
        console.log("上传失败")
      },
      {
        uploadURL: 'https://up.qiniup.com',
        region: 'ECN',

        domain: 'oxk5a6sj2.bkt.clouddn.com',
        uptokenURL: 'https://www.liuxuanlong.xyz/fenda/wx/upToken'
      }
    );
  }

}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgSrc:"../image/keyboard.png",
    button:"display",
    input:"none",
    remind:"",
    trylisten:"none",
    listenfont:"",
    tryimage:"none",
    textarea:"none",
    areaname:"编辑文字",
    usertext:"",
    modal:true,
    modal1:true,
    toast:"none",
    toasttext:"",
    value:value,
    minute:0,
    second1:0,
    second2:0,
    howSay:"none",
    sayfont:"按住 说话",
    headImg:"",
    userImg:"",
    info:"",
    useImgNum:"",
    userImgDisplay:"",
    NoshowQue: 'display',
    showQue: 'none',
    autoHeight:true,
    focus:false,
   
    
   
  },
  changeHeight:function(e){
    console.log(e.detail);
   
    if(e.detail.lineCount>5){
      this.setData(
        {
          autoHeight:false
        }
      );
    }
    else{
      this.setData(
        {
          autoHeight:true
        }
      );
    }
  },
  prewuserImg:function(){
    wx.previewImage({
      urls: userImg,
    })
  },
  figuretime:function(){
    voicetimer=setInterval(this.addvoicetime,1000);
  },
  addvoicetime:function(){
    second2++;
    if(second2==10){
      second2=0;
      second1++;
    }
    if(second1==6){
      second1=0;
      minute++;
    }
    this.setData(
      {
        minute:minute,
        second1:second1,
        second2:second2
      }
    );
  },
  showinput:function(e){
    value=e.detail.value;
    this.setData(
      {
        value:value
      }
    );
  },
  giveHeight:function(e){
    var length=e.detail.value.length;
    var row=length/13;
    var height=row*19;
    this.setData(
      {
        textHeight:height
      }
    );

  },
  changeway:function(){
    if(this.data.button=='display'){
      this.setData(
        {
          imgSrc:"../image/mic.png",
          button:"none",
          input:"display",
          focus:true
          //icons:'none'
          
        }
      );
     
    }
    else{
      this.setData(
        {
          imgSrc:"../image/keyboard.png",
          button:"display",
          input:"none",
          focus:false
          //icons:'display'
        
        }
      );
     
    }
  },
  submit1:function(){
    this.setData(
      {
        modal1:true
      }
    );
  },
  cancel:function(){
    this.setData(
      {
        modal:true,
        
      }
    );
  },
  hiddentoast:function(){
    this.setData(
      {
        toast:"none"
      }
    );
  },
  sendvoice:function(){         //发送语音
    if(pathvoice!=""){
    uploadvoice(pathvoice)
    pathvoice="";
    console.log("发送语音")
    }
  },
  sendimage:function(){
    if(pathimg!=""){
    uploadimage(pathimg);
    pathimg=[];
    console.log("发送图片")
    }
  },
  submit:function(){                                                                 //正式提交
    var that=this;
    console.log(voicetime)
    var times = time.formatTime(new Date());
    var StardTime=Date.now();
    wx.getStorage({
      key: 'login_uuId',
      success: function(res) {
          wxNum=res.data;
          wx.request({
            url: "https://www.liuxuanlong.xyz/fenda/wx/addAnswer",
            data: {
              questionId: questionId,
              wxNumber: wxNum,
              info: answers,
              recordUrl: pathvoice,
              imgUrl: pathimg,
              getDate: times,
              recordTime: voicetime,
              answerTime: StardTime

            },
            method: 'GET',
            success: function (res) {
              voicetime = 0;
              that.sendvoice();
              that.sendimage();
              that.setData(
                {
                  toasttext: "提交成功！",
                  toast: "display"
                }
              );
              wx.redirectTo({
                url: '../answered/answered?id=' + questionId,
              })

            },
          })
      },
    })
    
    
    this.setData(
      {
        modal:true
      }
    );
   
    setTimeout(this.hiddentoast,1500)
   
    /*wx.request({
      url: "",
      data:{
        answer:answers,
        pathImg:pathimg,
        pathVoice:pathvoice
      },
      success:function(){
        //设置提交成功提示
        that.setData(
          {
            toasttext:"提交成功！",
            toast:"display"
          }
        );
      },
      fail:function(){
        //设置提交失败提示
        that.setData(
          {
            toastext:"断网了..",
            toast:"display"
          }
        );
      },
    })*/
  },
  getinformation:function(e){
    
    if(e.detail.value.answer!=""||voice!=0||pathimg!=""){
      answers=e.detail.value.answer;
      
      this.setData(
        {
          modal:false
        }
      );
    }
    else{
      this.setData(
        {
         modal1:false 
        }
      );
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  textarea:function(){
    this.setData(
      {
        textarea:"display"
      }
    );
  },
  confirmwrite:function(){
    this.setData(
      {
        areaname:"编辑修改",
        textarea:"none"
      }
    );
  },
  cancelwrite:function(){
    this.setData(
      {
        areaname:"编辑文字",
        usertext:"",
        textarea:"none"
      }
    );
  },
  say:function(){
    var that=this;
    timer=setInterval(this.addtime,1000);
    wx.startRecord({                //开始录音
      success:function(res){
          pathvoice=res.tempFilePath;
          
      },
    })
    
    touchmove=1;
    touchend=1;
    this.setData(
      {
        sayfont:"松开 结束",
        howSay:"display",
        remind:"录音中..."
      }
    );
    this.figuretime();
  },
  addtime:function(){
    voicetime++;
  },
  touchmove:function(e){
    if(touchmove==1){
      endX=e.touches[0].pageX;
      endY=e.touches[0].pageY;
      
    }
    
  },
  stopvoice:function(){
    this.setData(
      {
        listenfont:"点击播放"
      }
    );
  },
  trylisten:function(){             //试听
    var that=this;
    
    if(switchVoice==0){
      that.setData(
        {
          listenfont: "停止播放"
        }
      );
    wx.playVoice({
      filePath: pathvoice,
      success:function(){
        
        
      },
     
    });
    switchVoice = 1;
    endvoice=setTimeout(that.stopvoice,1000*voicetime)
    }
    else{
      wx.stopVoice();
      that.setData(
        {
          listenfont:"点击播放"
        }
      );
      switchVoice=0;
      clearTimeout(endvoice)
    }
  },
  icon1:function(){             //删除录音
    voice=0;
    wx.stopVoice();
    this.setData(
      {
        remind:"录音已删除",
        trylisten:"none",
      }
    );
    
    wx.removeSavedFile({
      filePath:pathvoice ,
    })
    pathvoice="";
  },
  
  touchend:function(){          //离开触屏
    
    if(touchend==1){
      wx.stopRecord();
      clearInterval(timer);
    if(/*endX<70||endX>270||*/endY<520 /*||endY>600*/){
      
      this.setData(
        {
          remind:"录音已取消",
          howSay:"none"
        }
      );
      wx.removeSavedFile({
        filePath: pathvoice,
      });
      pathvoice="";
      voice=0;
      
    }
    else{
      if (((60 * minute + 10 * second1 + second2) > 3)){
      this.setData(
        {
          remind:"录音成功",
          trylisten:"display",
          listenfont:"点击试听",
          howSay: "none"
        }
      );
      voice=1;
    }
    else{
      this.setData(
        {
          remind:"录音时间过短",
          howSay:"none"
        }
      );
    }
    }
    }
    clearInterval(voicetimer);
    minute=0;
    second1=0;
    second2=0;
    this.setData(
      {
        sayfont:"按住 说话",
        minute:minute,
        second1:second1,
        second2:second2
      }
    );
    touchend=0;
    touchmove=0;
    endX=200;
    endY=586;
   
  },
  icon2:function(e){
    var index=e.currentTarget.id;
    pathimg.splice(index,1);
    var path=pathimg;
    image=0;
    this.setData(
      {
        userimg:path,
        remind:"图片已删除"
      }
    );
    
  },
  userimg:function(){
    var that=this;
    wx.chooseImage({
      count:3-pathimg.length,
      success: function(res) {
        pathimg=pathimg.concat (res.tempFilePaths);
        image=1;
        that.setData(
          {
            userimg:pathimg,
            tryimage:"display",
            remind:"图片选择成功"
          }
        );
      },
    })
  },
  lookImg:function(e){
    var index=e.currentTarget.id;
    var path=[];
    path.push(this.data.userPhoto[index]);
    for(var i=0;i<this.data.userPhoto.length;i++)
    if(i!=index)
    path.push(this.data.userPhoto[i]);
    wx.previewImage({
      urls: path,
    })
    
  },
  prewimg:function(e){
    var index=e.currentTarget.id;
    var path=[];
    path.push(pathimg[index]);
    for(var i=0;i<pathimg.length;i++)
    if(i!=index)
    path.push(pathimg[i]);
    wx.previewImage({
      urls: path,
    })
  },
  question:function(id){
    var that=this;
    questionId=id;
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/findQuestionByIdOrUuName",
      data:{
        id:id
        },
      method:'GET',
      success:function(res){
        console.log(res.data,'加载的问题');
       console.log(res.data.questionImgUrl);
        userImg=res.data.questionImgUrl;
        that.setData(
          {
            headImg:res.data.user.headImg,
            //userImg:res.data.questionImgUrl[0],
            info:res.data.info,
            //userImgNum:userImg.length,
            userPhoto:res.data.questionImgUrl,
            NoshowQue: 'none',
            showQue: 'display'
          }
        );
      },
    })
  },
  onLoad: function (options) {
    pathimg=[];
      var id=options.id;
      this.question(id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})